/**
 * Package name:indi.yangshenhui.db
 * File name:DataSourceUtil.java
 * Date:2016年8月12日-上午9:30:38
 * feiniu.com Inc.Copyright (c) 2013-2015 All Rights Reserved.
 *
 */
package indi.yangshenhui.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @ClassName DataSourceUtil
 * @Description 获取数据库连接
 * @date 2016年8月12日 上午9:30:38
 * @author shenhui.yang
 * @version 1.0.0
 *
 */
public class DataSourceUtil {
	private static final Logger log = LoggerFactory.getLogger(DataSourceUtil.class);
	private static Properties properties;
	private static DruidDataSource dataSource;
	private static ThreadLocal<Connection> container;

	/**
	 * 关闭连接 close void
	 * 
	 * @exception @since
	 *                1.0.0
	 */
	public static void close() {
		try {
			Connection conn = container.get();
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			try {
				container.remove();
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 回滚事务 rollback void
	 * 
	 * @exception @since
	 *                1.0.0
	 */
	public static void rollback() {
		try {
			Connection conn = container.get();
			if (conn != null) {
				conn.rollback();
				container.remove();
			}
		} catch (Exception e) {
			log.error("回滚事务异常:" + e.getMessage(), e);
		}
	}

	/**
	 * 提交事务 commit void
	 * 
	 * @exception @since
	 *                1.0.0
	 */
	public static void commit() {
		try {
			Connection conn = container.get();
			if (null != conn) {
				conn.commit();
			}
		} catch (Exception e) {
			log.error("提交事务异常:" + e.getMessage(), e);
		}
	}

	/**
	 * 获取当前线程上的连接开启事务 startTransaction void
	 * 
	 * @exception @since
	 *                1.0.0
	 */
	public static void startTransaction() {
		Connection conn = container.get();
		if (conn == null) {
			conn = getConnection();
			container.set(conn);
		}
		try {
			conn.setAutoCommit(false);
		} catch (Exception e) {
			log.error("开启事务异常:" + e.getMessage(), e);
		}
	}

	/**
	 * 获取数据库连接 getConnection
	 * 
	 * @return Connection
	 * @exception @since
	 *                1.0.0
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			container.set(conn);
		} catch (Exception e) {
			log.error("获取数据库连接异常:" + e.getMessage(), e);
		}
		return conn;
	}

	private static void init() {
		log.info("初始化Druid数据库连接池");
		if (dataSource == null) {
			dataSource = new DruidDataSource();
		}
		if (container == null) {
			container = new ThreadLocal<Connection>();
		}
		dataSource.setDriverClassName(properties.getProperty("mysql.connection.driverclass"));
		dataSource.setUrl(properties.getProperty("mysql.connection.url"));
		dataSource.setUsername(properties.getProperty("mysql.connection.username"));
		dataSource.setPassword(properties.getProperty("mysql.connection.password"));
		dataSource.setInitialSize(2);
		dataSource.setMaxActive(20);
		dataSource.setMinIdle(0);
		dataSource.setMaxWait(60000);
		dataSource.setValidationQuery("SELECT 1");
		dataSource.setTestOnBorrow(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setPoolPreparedStatements(false);
	}

	public static void loadProperties(String prop) {
		log.info("加载配置文件[" + prop + "]");
		if (properties == null) {
			properties = PropertiesUtil.getInstance().getProperties(prop);
			init();
		}
	}

	private DataSourceUtil() {
	}

	public static void main(String[] args) {
		loadProperties("application.properties");
	}

	public static DruidDataSource getDataSource() {
		return dataSource;
	}

}
